package com.zhuangjie.shop;

import java.time.LocalTime;

public class TimeValidator {
    private void checkTimeValid(boolean isTomorrow, String startTime, String endTime, String validTitle) {
        if (startTime == null || endTime == null) {
            throw new IllegalArgumentException(validTitle + "时间不能为空！");
        }
        LocalTime startLocalTime, endLocalTime;
        try {
            startLocalTime = LocalTime.parse(startTime);
            endLocalTime = LocalTime.parse(endTime);
        } catch (Exception e) {
            throw new IllegalArgumentException(validTitle + "时间格式错误！");
        }
        if (!isTomorrow && startLocalTime.isAfter(endLocalTime)) {
            throw new IllegalArgumentException(validTitle + "开始时间不能晚于结束时间！");
        }
    }

    public static void main(String[] args) {
        TimeValidator validator = new TimeValidator();

        // 示例测试
        try {
            validator.checkTimeValid(false, "08:00", "18:00", "Work Hours");
            System.out.println("Valid time range.");
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }

        try {
            validator.checkTimeValid(true, "23:00", "02:00", "Overnight Shift");
            System.out.println("Valid time range.");
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }
    }
}
